package main;

import java.awt.*;

import static main.Register.ENTITIES;
import static main.Register.WALLS;

public class ProjectileEntity extends AbstractEntity {
    public Image left;
    public Image right;
    public boolean trueToMoveX = false;
    public boolean trueToMoveY = false;



    public ProjectileEntity(Image left,Image right) {
        super(right);
        this.right = right;
        this.left = left;
    }

    @Override
    public void update() {
        super.update();

    }


    public void flyTo (double x, double y) {
        double stepx;
        double stepy;


        x = x - posX;
        y = y - posY;


        double c = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));

        stepx = x;
        stepy = y;

        turn = stepx == 0 ? Integer.compare((int) stepy, 0) : Integer.compare((int) stepx, 0);

        if(moveIsTrue(stepx, stepy)) {

            posX += stepx * 3 / c;
            posY += stepy * 3 / c;

        }

    }
    public boolean moveIsTrue (double stepx, double stepy) {
        boolean isIn = false;
//        for (LivingEntity wall : ENTITIES) {
//            if ((this.posX + this.width - 1 + stepx < wall.boxX1)
//                    || (this.posX + stepx + 1 > wall.boxX2)
//                    || (this.posY + this.height - 1 + stepy < wall.boxY1)
//                    || (this.posY + stepy + 1 > wall.boxY2)
//            ) {
//                continue;
//            }
//            isIn = true;
//            break;
//        }


        if (!isIn) {
            for (AbstractEntity wall : WALLS) {
                if ((this.posX + this.width - 1 + stepx < wall.boxX1)
                        || (this.posX + stepx + 1 > wall.boxX2)
                        || (this.posY + this.height - 1 + stepy < wall.boxY1)
                        || (this.posY + stepy + 1 > wall.boxY2)
                ) {
                    continue;
                }
                isIn = true;
                break;
            }
        }
        return !isIn;
    }
}
